/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package szachy;

import java.io.Serializable;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Robert
 */
public class Wieza extends Figura implements Serializable{
    private static final long serialVersionUID = 1L;
    public Wieza(){
    };
    public Wieza(char kolor, String nazwa, String rodzaj){
        this.kolor=kolor;
        iloscRuchu=8;
        this.nazwa=nazwa;
        this.rodzaj=rodzaj;
    }
   public boolean mozliwyRuch(int fromX, int fromY, int toX, int toY,IGra g){
    //ustalamy kierunek ruchu
        boolean answer = true;
        
        if(fromX==toX){//ruch względem pinou
            if(fromY>toY){
                for(int i=fromY-1;i>=toY;i--){
                    try {
                        if (g.CzyZajete(toX, i)) {
                            answer = false;
                        }
                    } catch (RemoteException ex) {
                        Logger.getLogger(Wieza.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            } else{
                for(int i=fromY+1;i<=toY;i++){
                    try {
                        if (g.CzyZajete(toX, i)) {
                            answer = false;
                        }
                    } catch (RemoteException ex) {
                        Logger.getLogger(Wieza.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }else if (fromY==toY){//ruch względem poziomu
            if(fromX>toX){
                for(int i=fromX-1;i>=toX;i--){
                    try {
                        if (g.CzyZajete(i, toY)) {
                            answer = false;
                        }
                    } catch (RemoteException ex) {
                        Logger.getLogger(Wieza.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            } else{
                for(int i=fromX+1;i<=toX;i++){
                    try {
                        if (g.CzyZajete(i, toY)) {
                            answer = false;
                        }
                    } catch (RemoteException ex) {
                        Logger.getLogger(Wieza.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        } else //ruch nie jest wykonywany po linii
            answer=false;
        return answer;
    }
    public boolean mozliwoscBicia(int fromX, int fromY, int toX, int toY,IGra g){
        try {
            if (g.ZwrocFigure(toX, toY).CoTo().equals("krol")) {
                return false;
            }
        } catch (RemoteException ex) {
            Logger.getLogger(Pion.class.getName()).log(Level.SEVERE, null, ex);
        }
        if(fromX==toX)
            if(fromY>toY) return mozliwyRuch(fromX, fromY, toX, toY+1, g); //sprawdzenie czy wieza moze dotrzec do tego miejsca
            else return mozliwyRuch(fromX, fromY, toX, toY-1, g);
        else {
            if(fromX>toX) return mozliwyRuch(fromX, fromY, toX+1, toY, g);
            else return mozliwyRuch(fromX, fromY, toX-1, toY, g);
        }
    }
}
